<script setup>
import { ref, onMounted } from 'vue'
import { useHealthStore } from '../stores/healthStore'
import StatsChart from '../components/StatsChart.vue'

const store = useHealthStore()
const statsData = ref(null)

onMounted(() => {
  statsData.value = store.getWeeklyData()
})
</script>

<template>
  <div class="stats-view">
    <h1>健康数据统计</h1>
    <p class="subtitle">最近7天的健康数据趋势分析</p>
    
    <div v-if="statsData" class="charts">
      <div class="chart-container">
        <h2>运动时长统计</h2>
        <StatsChart 
          :labels="statsData.labels" 
          :data="statsData.exerciseData" 
          label="运动时长（分钟）"
          color="#4CAF50" />
      </div>
      
      <div class="chart-container">
        <h2>卡路里摄入统计</h2>
        <StatsChart 
          :labels="statsData.labels" 
          :data="statsData.calorieData" 
          label="卡路里（大卡）"
          color="#FF9800" />
      </div>
      
      <div class="chart-container">
        <h2>睡眠时长统计</h2>
        <StatsChart 
          :labels="statsData.labels" 
          :data="statsData.sleepData" 
          label="睡眠时长（小时）"
          color="#2196F3" />
      </div>
    </div>
    
    <div v-else class="loading">
      <p>加载数据中...</p>
    </div>
    
    <div class="insights">
      <h2>数据分析洞察</h2>
      <div class="insight-card">
        <div class="icon">💡</div>
        <div class="content">
          <h3>运动建议</h3>
          <p>根据您的运动记录，建议每周增加1-2次有氧运动，每次30分钟以上。</p>
        </div>
      </div>
      <div class="insight-card">
        <div class="icon">🍎</div>
        <div class="content">
          <h3>饮食建议</h3>
          <p>您的饮食摄入较为均衡，建议增加蔬菜水果比例，减少高糖食品。</p>
        </div>
      </div>
      <div class="insight-card">
        <div class="icon">😴</div>
        <div class="content">
          <h3>睡眠建议</h3>
          <p>您的睡眠质量良好，建议保持规律的睡眠时间，避免睡前使用电子设备。</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.stats-view {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

h1 {
  color: #2c3e50;
  margin-bottom: 5px;
}

.subtitle {
  color: #7f8c8d;
  margin-top: 0;
  margin-bottom: 30px;
  font-size: 18px;
}

.charts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 25px;
  margin-bottom: 40px;
}

.chart-container {
  background: white;
  border-radius: 16px;
  padding: 25px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.chart-container h2 {
  margin-top: 0;
  color: #2c3e50;
  font-size: 20px;
}

.loading {
  text-align: center;
  padding: 50px;
  font-size: 18px;
  color: #7f8c8d;
}

.insights {
  background: white;
  border-radius: 16px;
  padding: 30px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.insights h2 {
  color: #2c3e50;
  margin-top: 0;
}

.insight-card {
  display: flex;
  gap: 20px;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
}

.insight-card:last-child {
  border-bottom: none;
}

.insight-card .icon {
  font-size: 32px;
  width: 60px;
  height: 60px;
  background: #f8f9fa;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.insight-card .content h3 {
  margin-top: 0;
  margin-bottom: 10px;
  color: #2c3e50;
}

.insight-card .content p {
  margin: 0;
  color: #555;
  line-height: 1.6;
}

@media (max-width: 768px) {
  .charts {
    grid-template-columns: 1fr;
  }
}
</style>